home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / DIFIND.C < prev    next >
C/C++ Source or Header  |  1995-05-24  |  6KB  |  179 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    difind.c
  5. //   Title:    Data File I/O Library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains to locate files based on names. These are basically
  25. //    helper routines for the open functions.
  26. //
  27. //    The code in this module should be written entirely in C. 
  28. //    Do not use any C++ constructs.
  29. //
  30. //    This module is portable to:
  31. //        DOS 3.X+
  32. //        MS Windows 3.X+
  33. //        OS/2 2.X+
  34. //        OS/2 2.0 PM
  35. //        SCO UNIX.
  36. //
  37. //    The following compilers are supported:
  38. //        MSC 6.0A
  39. //        MSC/C++ 7.0
  40. //        Borland C++ 3.1 for DOS
  41. //        Borland C++ 1.0 for OS/2 2.X
  42. //        SCO UNIX cc
  43. //
  44. //----------------------------------------------------------------------------
  45. #include <di.h>
  46.  
  47.  
  48. //----------------------------------------------------------------------------
  49. //   Description:    Locate logical file entry. 
  50. //    Parameters:pcsz     Full file name.
  51. //         usType    File type to open.
  52. //         phpf    Variable to receive physical file handle
  53. //         pcDir    Variable to receive offset of directory entry
  54. //         pdir    Buffer to receive directory entry
  55. //       Returns:   TRUE if successful.
  56. //----------------------------------------------------------------------------
  57. BOOL FN_E DioFindLogical(PCSZ pcsz, USHORT usType, PHPF phpf, PSIZET pcDir, PDATADIR pdir)
  58. {
  59.     CHAR szPhysical[MAX_DIO_NAME+1];
  60.     CHAR szLogical[MAX_DIO_NAME+1];
  61.  
  62.  
  63.     if (!DioSplitName(pcsz, szPhysical, szLogical))
  64.         return FALSE;
  65.  
  66.     if (szPhysical[0])
  67.         {
  68.         if (!DioFindPhyiscal(szPhysical, phpf))
  69.             return FALSE;
  70.  
  71.         if (!DioFindLogicalSearch(szLogical, usType, *phpf, pcDir, pdir))
  72.             return FALSE;
  73.         }
  74.     Assert(phpf);
  75.     for (*phpf = 0; *phpf < MAX_PHYSICAL_FILES; ++(*phpf))
  76.         if (di.physical[*phpf].fUsed)
  77.             {
  78.             if (DioFindLogicalSearch(szLogical, usType, *phpf, pcDir, pdir))
  79.                 return TRUE;
  80.             }
  81.     return FALSE;                        
  82. }
  83.  
  84.  
  85. //----------------------------------------------------------------------------
  86. //   Description:    Search for logical file entry. 
  87. //    Parameters:    szLogical     Logical file name
  88. //            usType        File type to open.
  89. //        hpf            Physical file handle
  90. //        pcDir            Variable to receive offset of directory entry
  91. //        pdir            Buffer to receive directory entry
  92. //       Returns:    TRUE if successful.
  93. //----------------------------------------------------------------------------
  94. BOOL FN_E DioFindLogicalSearch(PCSZ pcszLogical, USHORT usType, HPF hpf, PSIZET pcDir, PDATADIR pdir)
  95. {
  96.     DATAHDR hdr;
  97.     CHAR szName[DFD_NAME+1];
  98.  
  99.     if (!DioHeaderRead(hpf, &hdr))
  100.         return FALSE;
  101.  
  102.     Assert(pcDir && pdir && pcszLogical);
  103.     for (*pcDir = 0; *pcDir < (SIZET)hdr.usDirectoryUsed; ++*pcDir)
  104.         {
  105.         if (!DioDirRead(hpf, *pcDir, pdir))
  106.             return FALSE;
  107.  
  108.         memset(szName,0,sizeof(szName));
  109.         memcpy(szName, pdir->chName, sizeof(pdir->chName));
  110.  
  111.         if (stricmp(szName, pcszLogical) == 0
  112.         && pdir->usType == usType)
  113.             return TRUE;
  114.         }
  115.      return FALSE;
  116. }
  117.  
  118.  
  119. //----------------------------------------------------------------------------
  120. //   Description:    Find physical file entry by name
  121. //    Parameters:    pcsz        Internal physical file name
  122. //                        phpf        Variable to variable to recieve file handle
  123. //       Returns:    TRUE if successful.
  124. //----------------------------------------------------------------------------
  125. BOOL FN_E DioFindPhyiscal(PCSZ pcsz, PHPF phpf)
  126. {
  127.     HPF hpf;
  128.  
  129.     Assert(pcsz);
  130.     Assert(pcsz[0] && strlen(pcsz) <= MAX_PHYSICAL_NAME);
  131.     Assert(phpf);
  132.     for (hpf = 0; hpf < MAX_PHYSICAL_FILES; ++hpf)
  133.         if (di.physical[hpf].fUsed
  134.         && stricmp(di.physical[hpf].szName, pcsz) == 0)
  135.             {
  136.             *phpf = hpf;
  137.             return TRUE;                        
  138.             }
  139.     Error("Physical file '%s' not found.", pcsz);
  140.     return FALSE;                        
  141. }
  142.  
  143.  
  144. //----------------------------------------------------------------------------
  145. //   Description:    Split a name into the physical and logical file name.
  146. //    Parameters:    pcsz                Full name
  147. //                        pszPhysical        Buffer to receive physical name
  148. //                        pszLogical        Buffer to receive logical name
  149. //       Returns:    TRUE if successful.
  150. //----------------------------------------------------------------------------
  151. BOOL FN_E DioSplitName(PCSZ pcsz, PSZ pszPhysical, PSZ pszLogical)
  152. {
  153.     PSZ ptr;
  154.  
  155.     Assert(pcsz);
  156.     Assert(pcsz[0] && strlen(pcsz) <= MAX_DIO_NAME);
  157.     Assert(pszPhysical && pszLogical);
  158.     if ((ptr = strchr(pcsz, '~')) != NULL)
  159.         {
  160.         *ptr = '\0';                            // Explicit physical name
  161.         Assert(strlen(pcsz) <= MAX_PHYSICAL_NAME);
  162.         strcpy(pszPhysical, pcsz);
  163.         *ptr = '~';
  164.         Assert(strlen(ptr + 1) <= MAX_LOGICAL_NAME);
  165.         strcpy(pszLogical, ptr + 1);
  166.         }
  167.     else
  168.         {
  169.         pszPhysical[0] = '\0';
  170.         Assert(strlen(pcsz) <= MAX_LOGICAL_NAME);
  171.         strcpy(pszLogical, pcsz);
  172.         }
  173.     Assert(pszLogical[0]);
  174.     return TRUE;
  175. }
  176. //----------------------------------------------------------------------------
  177. //------------------------------- End of File --------------------------------
  178. //----------------------------------------------------------------------------
  179.